<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/2/24
 * 描述:
 */

namespace app\service;

use app\events\ChatEvents;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Workerman\Worker;

class ChatWorkerService
{

    // 初始化gateway,一定要统一配配置
    public static function init_gateway()
    {
        \GatewayClient\Gateway::$registerAddress = config('worker.register_address') . ':' . config('worker.register_port');
        //bs_log(\GatewayClient\Gateway::$registerAddress, 'worker_init', 'worker_init.log');
    }

    public static function BusinessWorker()
    {
        $config = config('worker');
        // bussinessWorker 进程
        $worker = new BusinessWorker();
        // worker名称
        $worker->name = 'ChatBusinessWorker';
        $worker::$logFile = app()->getRuntimePath() . date('Ymd') . 'worker.log';
        $worker::$pidFile = app()->getRuntimePath() . 'worker.pid';
        Worker::$logFile = app()->getRuntimePath() . date('Ymd') . 'worker.log';
        Worker::$pidFile = app()->getRuntimePath() . 'worker.pid';
        // bussinessWorker进程数量
        // 服务注册地址
        $worker->registerAddress = $config['register_address'] . ':' . $config['register_port'];
        $worker->eventHandler = ChatEvents::class;// 事件处理类
        // 如果不是在根目录启动，则运行runAll方法
        if (!defined('GLOBAL_START')) {
            Worker::runAll();
        }
    }

    public static function Register()
    {
        $config = config('worker');
        // register 服务必须是text协议
        $register = new Register('text://' . $config['register_address'] . ':' . $config['register_port']);
        $register->name = 'ChatRegister';
        $register::$logFile = app()->getRuntimePath() . date('Ymd') . 'register.log';
        $register::$pidFile = app()->getRuntimePath() . 'register.pid';
        // 如果不是在根目录启动，则运行runAll方法
        if (!defined('GLOBAL_START')) {
            Worker::runAll();
        }

    }

    public static function Gateway()
    {
        $config = config('worker');
        // gateway 进程
        $gateway = new Gateway(config('worker.gateway_address'));
        $gateway::$logFile = app()->getRuntimePath() . date('Ymd') . 'gateway.log';
        $gateway::$pidFile = app()->getRuntimePath() . 'gateway.pid';
        // 设置名称，方便status时查看
        $gateway->name = 'ChatGateway';
        // 设置进程数，gateway进程数建议与cpu核数相同
        $gateway->count = $config['count'];
        // 分布式部署时请设置成内网ip（非127.0.0.1）
        $gateway->lanIp = $config['lan_ip'];
        // 内部通讯起始端口。假如$gateway->count=4，起始端口为2300
        // 则一般会使用2300 2301 2302 2303 4个端口作为内部通讯端口
        $gateway->startPort = $config['start_port'];

        // 心跳间隔
        $gateway->pingInterval = $config['ping_interval'];

        $gateway->pingNotResponseLimit = $config['ping_not_response_limit'];
        // 心跳数据(如果为空则前端需要设置心跳后端遍历是否存在)
        $gateway->pingData = '{"type":"ping"}';
        //$gateway->pingData = json_encode(['type' => 'ping','msg'=>'balala']);

        // 服务注册地址
        $gateway->registerAddress = $config['register_address'] . ':' . $config['register_port'];

        // 如果不是在根目录启动，则运行runAll方法
        if (!defined('GLOBAL_START')) {
            Worker::runAll();
        }

    }
}
